﻿using System;
using System.ComponentModel;

namespace Mbs
{
    [Serializable]
    public abstract class DisposableObject : IDisposable
    {
        private readonly object SynchRoot = new object();


        ~DisposableObject()
        {
            Dispose(false);
        }

        #region IDisposable Members

        public void Dispose()
        {
            lock (SynchRoot)
            {
                if (!IsDisposed)
                {
                    IsDisposed = true;
                    Dispose(true);
                    GC.SuppressFinalize(this);
                }
            }
        }

        #endregion

        [Browsable(false)]
        public bool IsDisposed { get; private set; }

        protected virtual void Dispose(bool disposing)
        {
        }

        protected virtual void CheckNotDisposed()
        {
            if (IsDisposed)
                throw new ObjectDisposedException(GetType().Name);
        }
    }
}
